//
//  MBGoodsDetailView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/5/4.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailView.h"
#import "MBGoodsDetailTitleView.h"
#import "MBScrollViewFooterTitleView.h"


@interface MBGoodsDetailView ()
@property (nonatomic,weak)MBGoodsDetailTitleView* titleView;
@property (nonatomic,weak)MBScrollViewFooterTitleView* footerTitleView;
@property (nonatomic,strong) MASConstraint* commentConstraint;
@property (nonatomic,strong) MASConstraint* footerViewConstraint;

@end

@implementation MBGoodsDetailView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = COLOR_BG;
        
        MBBannerView* bannerView = [[MBBannerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:375])];
        self.bannerView = bannerView;
        [self addSubview:bannerView];
        
        [bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, [UIView lf_sizeFromIphone6:375]));
        }];
        
        MBGoodsDetailTitleView* titleView = [[MBGoodsDetailTitleView alloc]init];
        self.titleView = titleView;
        [self addSubview:titleView];
        
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bannerView.mas_bottom);
            make.left.equalTo(self);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
        
        
        MBGoodsDetailCommentView* commentView = [[MBGoodsDetailCommentView alloc]init];
        self.commentView = commentView;
        [self addSubview:commentView];
        
        [commentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(self);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
        
        MBScrollViewFooterTitleView* footerTitleView = [[MBScrollViewFooterTitleView alloc]init];
        self.footerTitleView = footerTitleView;
        [self addSubview:footerTitleView];
        
        [footerTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
            self.commentConstraint = make.top.equalTo(commentView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:5]);
            make.top.equalTo(commentView.mas_top).priorityLow();
            make.left.equalTo(self);
        }];
        
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            self.footerViewConstraint = make.bottom.equalTo(footerTitleView);
            make.bottom.equalTo(footerTitleView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:11]).priorityLow();
        }];
    }
    return self;
}

-(void)setInfoModel:(MBGoodsInfoModel *)infoModel
{
    _infoModel = infoModel;
    
    id dic ;
    if (infoModel.gallery.count > 0) {
        dic = infoModel.gallery[0];
    }
    if (![dic isKindOfClass:[NSString class]]) {
        NSMutableArray* imageList = [NSMutableArray array];
        
        for (int i=0; i<infoModel.gallery.count; i++) {
            NSString* imageUrl = [infoModel.gallery[i] objectForKey:@"image_url"];
            [imageList addObject:imageUrl];
        }
        infoModel.gallery = imageList;
        self.bannerView.curImageArray = imageList;
    }else {
        self.bannerView.curImageArray = infoModel.gallery;
    }
    
    if (infoModel.comment.count > 1) {
        [self.commentConstraint activate];
        [self.footerViewConstraint activate];
        self.commentView.commentModel = infoModel.comment[0];
        self.commentView.hidden = NO;
    }else {
        [self.commentConstraint deactivate];
        [self.footerViewConstraint deactivate];
        self.commentView.hidden = YES;
    }
    self.titleView.goodsDetailModel = infoModel;
    self.commentView.infoModel = infoModel;
}


@end
